import { SubjectComponent } from "../../../core/subject";
import { Uid } from "../../../core/utils";

const { ccclass, property } = cc._decorator;


export type ArrData = any[];

export interface IOwnerShipObject {
    uuid: string;
    owner: string;
};

export interface IObjectFuncs {
    toArrData(): ArrData;
    fromArrData(dat: ArrData): void;
}


@ccclass
export default class CCObject extends SubjectComponent {
    toArrData(): any[] {
        let data: ArrData = [];
        data.push(this.node.name);
        data.push(this.owner);
        data.push(this.objUid);
        data.push(Math.floor(this.node.x));
        data.push(Math.floor(this.node.y));
        data.push(Math.floor(this.dv.x * 100));
        data.push(Math.floor(this.dv.y * 100));
        return data;
    }
    fromArrData(data: any[]): void {
        this.node.name = data.shift();
        this.owner = data.shift();
        this.objUid = data.shift();
        this.node.x = data.shift();
        this.node.y = data.shift();
        this.dv.x = data.shift() / 100;
        this.dv.y = data.shift() / 100;
    }


    static NewArrData(objName: string, owner: string, x: number, y: number, dX: number, dY: number, objUid = Uid.getUid()) {
        let data: any[] = [];
        data.push(objName);
        data.push(owner);
        data.push(objUid);
        data.push(Math.floor(x));
        data.push(Math.floor(y));
        data.push(Math.floor(dX * 100));
        data.push(Math.floor(dY * 100));
        return data;
    }

    @property
    speed: number = 0;

    dv: cc.Vec2 = cc.v2();
    owner = "";
    objUid = "";

    protected update(dt: number): void {
        if (!this.dv.equals(cc.Vec2.ZERO)) {
            let dv = this.dv.mul(dt * this.speed);
            this.node.x = cc.misc.clampf(this.node.x + dv.x, -this.node.parent.width / 2, this.node.parent.width / 2);
            this.node.y = cc.misc.clampf(this.node.y + dv.y, -this.node.parent.height / 2, this.node.parent.height / 2);
        }
    }
};